package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;

import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.CategoryVO;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class selectCateg extends Dialog {

	protected Object result;
	protected Shell shell;
	ArticleVO articulo;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public selectCateg(Shell parent, int style, ArticleVO article) {
		super(parent, style);
		setText("Seleccione la categoria");
		articulo=article;;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(407, 237);
		shell.setText(getText());
		
		final Combo combo = new Combo(shell, SWT.NONE);
		combo.setBounds(181, 77, 133, 22);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(25, 81, 59, 14);
		lblNewLabel.setText("Categorias");
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String cat = combo.getText();
				ArticleProxyMgt art = ServiceFacade.getInstance().getArticleProxyMgt();
				CategoryVO[] cats = null;
				try {
				
					cats = art.getCategories();
					System.out.println(cats.length);
				} catch (RemoteException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (ProxyException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				}
				CategoryVO c=null;
				for (int i = 0; i<cats.length;i++){
					if (cat.equalsIgnoreCase(cats[i].getCat())){
						c=cats[i];
					}
				}
				articulo.setCat(c);
				VerProductoMod m = new VerProductoMod (shell, shell.getStyle(), articulo);
				shell.setVisible(false);
				m.open();
				shell.close();
				
			}
		});
		btnAceptar.setBounds(73, 146, 94, 28);
		btnAceptar.setText("Aceptar");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnCancelar.setBounds(240, 146, 94, 28);
		btnCancelar.setText("Cancelar");
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		CategoryVO [] cats =uso.getCategories();
		System.out.println(cats.length);
		for (int i= 0;i<cats.length;i++){
			System.out.println(cats[i].getCat());
			combo.add(cats[i].getCat());
		}
	}

}
